//! Contains the [`Signature`] type

use crate::constants::{D_MESG, D_PBLC};
use crate::error::LmsDeserializeError;
use crate::ots::modes::LmsOtsMode;
use crate::types::Identifier;
use digest::Digest;
use hybrid_array::{Array, ArraySize};
use signature::SignatureEncoding;
use std::cmp::Ordering;
use std::ops::{Add, Mul};
use typenum::{Prod, Sum, U1, U4};

use super::VerifyingKey;

#[derive(Debug, Eq)]
/// Opaque struct representing a LM-OTS signature
pub struct Signature<Mode: LmsOtsMode> {
    pub(crate) c: digest::Output<Mode::Hasher>,
    pub(crate) y: Array<digest::Output<Mode::Hasher>, Mode::PLen>,
}

// manual implementation is required to not require bounds on Mode
impl<Mode: LmsOtsMode> Clone for Signature<Mode> {
    fn clone(&self) -> Self {
        Self {
            c: self.c.clone(),
            y: self.y.clone(),
        }
    }
}

// manual implementation is required to not require bounds on Mode
impl<Mode: LmsOtsMode> PartialEq for Signature<Mode> {
    fn eq(&self, other: &Self) -> bool {
        self.c == other.c && self.y == other.y
    }
}

/// Useful type alias to get the [`Array`] representation
pub type Output<Mode> =
    Array<u8, Sum<Prod<<Mode as LmsOtsMode>::NLen, Sum<<Mode as LmsOtsMode>::PLen, U1>>, U4>>;

/// Converts a [`Signature`] into its byte representation
impl<Mode: LmsOtsMode> From<Signature<Mode>> for Output<Mode>
where
    // required for Output<Mode>
    Mode::PLen: Add<U1>,
    Mode::NLen: Mul<Sum<Mode::PLen, U1>>,
    Prod<Mode::NLen, Sum<Mode::PLen, U1>>: Add<U4>,
    Sum<Prod<Mode::NLen, Sum<Mode::PLen, U1>>, U4>: ArraySize,
{
    fn from(sig: Signature<Mode>) -> Self {
        Array::try_from_iter(
            std::iter::empty()
                .chain(Mode::TYPECODE.to_be_bytes())
                .chain(sig.c.clone())
                .chain(sig.y.iter().flatten().cloned()),
        )
        .expect("ok")
    }
}

/// Tries to parse a [`Signature`] from an exact slice
impl<'a, Mode: LmsOtsMode> TryFrom<&'a [u8]> for Signature<Mode> {
    type Error = LmsDeserializeError;

    fn try_from(sig: &'a [u8]) -> Result<Self, Self::Error> {
        if sig.len() < 4 {
            return Err(LmsDeserializeError::NoAlgorithm);
        }

        let (alg, sig) = sig.split_at(4);

        // will never panic because alg is a 4 byte slice
        if u32::from_be_bytes(alg.try_into().unwrap()) != Mode::TYPECODE {
            return Err(LmsDeserializeError::WrongAlgorithm);
        }

        let expected = Mode::N * (Mode::P + 1);

        match sig.len().cmp(&expected) {
            Ordering::Less => Err(LmsDeserializeError::TooShort),
            Ordering::Greater => Err(LmsDeserializeError::TooLong),
            Ordering::Equal => {
                // sig is now guaranteed to be of the form C || y[0] || ... || y[p - 1]
                let (c, y) = sig.split_at(Mode::N);

                let c = Array::try_from(c).expect("size invariant violation");

                // ys is an iterator over Array<u8, Mode::NLen> of length p
                let ys = y
                    .chunks_exact(Mode::N)
                    .map(|chunk| Array::try_from(chunk).expect("size invariant violation"));
                debug_assert!(ys.len() == Mode::P);
                let y = Array::from_iter(ys);

                Ok(Self { c, y })
            }
        }
    }
}

impl<Mode: LmsOtsMode> Signature<Mode> {
    /// Returns a public key candidate for this signature as defined by
    /// algorithm 4b of the LMS RFC. The signature will always be valid for
    /// the returned public key candidate.
    pub fn recover_pubkey(&self, id: Identifier, q: u32, msg: &[u8]) -> VerifyingKey<Mode> {
        // algorithm 4b

        // Q = H(I || u32str(q) || u16str(D_MESG) || C || message)
        let msg_hash = Mode::Hasher::new()
            .chain_update(id)
            .chain_update(q.to_be_bytes())
            .chain_update(D_MESG)
            .chain_update(&self.c)
            .chain_update(msg)
            .finalize();

        // first part of
        // Kc = H(I || u32str(q) || u16str(D_PBLC) || z[0] || z[1] || ... || z[p-1])
        let mut hasher = Mode::Hasher::new()
            .chain_update(id)
            .chain_update(q.to_be_bytes())
            .chain_update(D_PBLC);

        let mut tmp = Array::default();
        for (i, a) in Mode::expand(&msg_hash).into_iter().enumerate() {
            tmp.clone_from(&self.y[i]);

            // for ( j = a; j < 2^w - 1; j = j + 1 )
            for j in a..((1usize << Mode::W) - 1) as u8 {
                // tmp = H(I || u32str(q) || u16str(i) || u8str(j) || tmp)
                Mode::Hasher::new()
                    .chain_update(id)
                    .chain_update(q.to_be_bytes())
                    .chain_update((i as u16).to_be_bytes())
                    .chain_update(j.to_be_bytes())
                    .chain_update(&tmp)
                    .finalize_into(&mut tmp);
            }
            // z[i] = tmp
            // except we directly absorb into the hash function to finish off the computation of
            // Kc = H(I || u32str(q) || u16str(D_PBLC) || z[0] || z[1] || ... || z[p-1])
            hasher.update(&tmp);
        }
        VerifyingKey {
            id,
            q,
            k: hasher.finalize(),
        }
    }
}

impl<Mode: LmsOtsMode> SignatureEncoding for Signature<Mode>
where
    // required for Output<Mode>
    Mode::PLen: Add<U1>,
    Mode::NLen: Mul<Sum<Mode::PLen, U1>>,
    Prod<Mode::NLen, Sum<Mode::PLen, U1>>: Add<U4>,
    Sum<Prod<Mode::NLen, Sum<Mode::PLen, U1>>, U4>: ArraySize,
{
    type Repr = Output<Mode>;
}

#[cfg(test)]
mod tests {
    use crate::ots::modes::{LmsOtsSha256N32W4, LmsOtsSha256N32W8};
    use crate::ots::signature::{LmsDeserializeError, Signature};

    #[test]
    fn test_deserialize_sha256_n32_w8() {
        let sig_str = "00000004\
                   0703c491e7558b35011ece3592eaa5da4d918786771233e8353bc4f62323185c\
                   95cae05b899e35dffd717054706209988ebfdf6e37960bb5c38d7657e8bffeef\
                   9bc042da4b4525650485c66d0ce19b317587c6ba4bffcc428e25d08931e72dfb\
                   6a120c5612344258b85efdb7db1db9e1865a73caf96557eb39ed3e3f426933ac\
                   9eeddb03a1d2374af7bf77185577456237f9de2d60113c23f846df26fa942008\
                   a698994c0827d90e86d43e0df7f4bfcdb09b86a373b98288b7094ad81a0185ac\
                   100e4f2c5fc38c003c1ab6fea479eb2f5ebe48f584d7159b8ada03586e65ad9c\
                   969f6aecbfe44cf356888a7b15a3ff074f771760b26f9c04884ee1faa329fbf4\
                   e61af23aee7fa5d4d9a5dfcf43c4c26ce8aea2ce8a2990d7ba7b57108b47dabf\
                   beadb2b25b3cacc1ac0cef346cbb90fb044beee4fac2603a442bdf7e507243b7\
                   319c9944b1586e899d431c7f91bcccc8690dbf59b28386b2315f3d36ef2eaa3c\
                   f30b2b51f48b71b003dfb08249484201043f65f5a3ef6bbd61ddfee81aca9ce6\
                   0081262a00000480dcbc9a3da6fbef5c1c0a55e48a0e729f9184fcb1407c3152\
                   9db268f6fe50032a363c9801306837fafabdf957fd97eafc80dbd165e435d0e2\
                   dfd836a28b354023924b6fb7e48bc0b3ed95eea64c2d402f4d734c8dc26f3ac5\
                   91825daef01eae3c38e3328d00a77dc657034f287ccb0f0e1c9a7cbdc828f627\
                   205e4737b84b58376551d44c12c3c215c812a0970789c83de51d6ad787271963\
                   327f0a5fbb6b5907dec02c9a90934af5a1c63b72c82653605d1dcce51596b3c2\
                   b45696689f2eb382007497557692caac4d57b5de9f5569bc2ad0137fd47fb47e\
                   664fcb6db4971f5b3e07aceda9ac130e9f38182de994cff192ec0e82fd6d4cb7\
                   f3fe00812589b7a7ce515440456433016b84a59bec6619a1c6c0b37dd1450ed4\
                   f2d8b584410ceda8025f5d2d8dd0d2176fc1cf2cc06fa8c82bed4d944e71339e\
                   ce780fd025bd41ec34ebff9d4270a3224e019fcb444474d482fd2dbe75efb203\
                   89cc10cd600abb54c47ede93e08c114edb04117d714dc1d525e11bed8756192f\
                   929d15462b939ff3f52f2252da2ed64d8fae88818b1efa2c7b08c8794fb1b214\
                   aa233db3162833141ea4383f1a6f120be1db82ce3630b3429114463157a64e91\
                   234d475e2f79cbf05e4db6a9407d72c6bff7d1198b5c4d6aad2831db61274993\
                   715a0182c7dc8089e32c8531deed4f7431c07c02195eba2ef91efb5613c37af7\
                   ae0c066babc69369700e1dd26eddc0d216c781d56e4ce47e3303fa73007ff7b9\
                   49ef23be2aa4dbf25206fe45c20dd888395b2526391a724996a44156beac8082\
                   12858792bf8e74cba49dee5e8812e019da87454bff9e847ed83db07af3137430\
                   82f880a278f682c2bd0ad6887cb59f652e155987d61bbf6a88d36ee93b6072e6\
                   656d9ccbaae3d655852e38deb3a2dcf8058dc9fb6f2ab3d3b3539eb77b248a66\
                   1091d05eb6e2f297774fe6053598457cc61908318de4b826f0fc86d4bb117d33\
                   e865aa805009cc2918d9c2f840c4da43a703ad9f5b5806163d7161696b5a0adc";
        let sig_bytes = hex::decode(sig_str).unwrap();
        let mut long_bytes = sig_bytes.clone();
        long_bytes.push(0);

        let sig = Signature::<LmsOtsSha256N32W8>::try_from(&sig_bytes[..]);
        let sig_wrongalgo = Signature::<LmsOtsSha256N32W4>::try_from(&sig_bytes[..]);
        let sig_short = Signature::<LmsOtsSha256N32W8>::try_from(&sig_bytes[..sig_bytes.len() - 1]);
        let sig_long = Signature::<LmsOtsSha256N32W8>::try_from(&long_bytes[..]);

        assert!(sig.is_ok());
        assert_eq!(sig_wrongalgo, Err(LmsDeserializeError::WrongAlgorithm));
        assert_eq!(sig_short, Err(LmsDeserializeError::TooShort));
        assert_eq!(sig_long, Err(LmsDeserializeError::TooLong));

        let ec: [u8; 32] = [
            0x07, 0x03, 0xc4, 0x91, 0xe7, 0x55, 0x8b, 0x35, 0x01, 0x1e, 0xce, 0x35, 0x92, 0xea,
            0xa5, 0xda, 0x4d, 0x91, 0x87, 0x86, 0x77, 0x12, 0x33, 0xe8, 0x35, 0x3b, 0xc4, 0xf6,
            0x23, 0x23, 0x18, 0x5c,
        ];
        let ey: [[u8; 32]; 34] = [
            [
                0x95, 0xca, 0xe0, 0x5b, 0x89, 0x9e, 0x35, 0xdf, 0xfd, 0x71, 0x70, 0x54, 0x70, 0x62,
                0x09, 0x98, 0x8e, 0xbf, 0xdf, 0x6e, 0x37, 0x96, 0x0b, 0xb5, 0xc3, 0x8d, 0x76, 0x57,
                0xe8, 0xbf, 0xfe, 0xef,
            ],
            [
                0x9b, 0xc0, 0x42, 0xda, 0x4b, 0x45, 0x25, 0x65, 0x04, 0x85, 0xc6, 0x6d, 0x0c, 0xe1,
                0x9b, 0x31, 0x75, 0x87, 0xc6, 0xba, 0x4b, 0xff, 0xcc, 0x42, 0x8e, 0x25, 0xd0, 0x89,
                0x31, 0xe7, 0x2d, 0xfb,
            ],
            [
                0x6a, 0x12, 0x0c, 0x56, 0x12, 0x34, 0x42, 0x58, 0xb8, 0x5e, 0xfd, 0xb7, 0xdb, 0x1d,
                0xb9, 0xe1, 0x86, 0x5a, 0x73, 0xca, 0xf9, 0x65, 0x57, 0xeb, 0x39, 0xed, 0x3e, 0x3f,
                0x42, 0x69, 0x33, 0xac,
            ],
            [
                0x9e, 0xed, 0xdb, 0x03, 0xa1, 0xd2, 0x37, 0x4a, 0xf7, 0xbf, 0x77, 0x18, 0x55, 0x77,
                0x45, 0x62, 0x37, 0xf9, 0xde, 0x2d, 0x60, 0x11, 0x3c, 0x23, 0xf8, 0x46, 0xdf, 0x26,
                0xfa, 0x94, 0x20, 0x08,
            ],
            [
                0xa6, 0x98, 0x99, 0x4c, 0x08, 0x27, 0xd9, 0x0e, 0x86, 0xd4, 0x3e, 0x0d, 0xf7, 0xf4,
                0xbf, 0xcd, 0xb0, 0x9b, 0x86, 0xa3, 0x73, 0xb9, 0x82, 0x88, 0xb7, 0x09, 0x4a, 0xd8,
                0x1a, 0x01, 0x85, 0xac,
            ],
            [
                0x10, 0x0e, 0x4f, 0x2c, 0x5f, 0xc3, 0x8c, 0x00, 0x3c, 0x1a, 0xb6, 0xfe, 0xa4, 0x79,
                0xeb, 0x2f, 0x5e, 0xbe, 0x48, 0xf5, 0x84, 0xd7, 0x15, 0x9b, 0x8a, 0xda, 0x03, 0x58,
                0x6e, 0x65, 0xad, 0x9c,
            ],
            [
                0x96, 0x9f, 0x6a, 0xec, 0xbf, 0xe4, 0x4c, 0xf3, 0x56, 0x88, 0x8a, 0x7b, 0x15, 0xa3,
                0xff, 0x07, 0x4f, 0x77, 0x17, 0x60, 0xb2, 0x6f, 0x9c, 0x04, 0x88, 0x4e, 0xe1, 0xfa,
                0xa3, 0x29, 0xfb, 0xf4,
            ],
            [
                0xe6, 0x1a, 0xf2, 0x3a, 0xee, 0x7f, 0xa5, 0xd4, 0xd9, 0xa5, 0xdf, 0xcf, 0x43, 0xc4,
                0xc2, 0x6c, 0xe8, 0xae, 0xa2, 0xce, 0x8a, 0x29, 0x90, 0xd7, 0xba, 0x7b, 0x57, 0x10,
                0x8b, 0x47, 0xda, 0xbf,
            ],
            [
                0xbe, 0xad, 0xb2, 0xb2, 0x5b, 0x3c, 0xac, 0xc1, 0xac, 0x0c, 0xef, 0x34, 0x6c, 0xbb,
                0x90, 0xfb, 0x04, 0x4b, 0xee, 0xe4, 0xfa, 0xc2, 0x60, 0x3a, 0x44, 0x2b, 0xdf, 0x7e,
                0x50, 0x72, 0x43, 0xb7,
            ],
            [
                0x31, 0x9c, 0x99, 0x44, 0xb1, 0x58, 0x6e, 0x89, 0x9d, 0x43, 0x1c, 0x7f, 0x91, 0xbc,
                0xcc, 0xc8, 0x69, 0x0d, 0xbf, 0x59, 0xb2, 0x83, 0x86, 0xb2, 0x31, 0x5f, 0x3d, 0x36,
                0xef, 0x2e, 0xaa, 0x3c,
            ],
            [
                0xf3, 0x0b, 0x2b, 0x51, 0xf4, 0x8b, 0x71, 0xb0, 0x03, 0xdf, 0xb0, 0x82, 0x49, 0x48,
                0x42, 0x01, 0x04, 0x3f, 0x65, 0xf5, 0xa3, 0xef, 0x6b, 0xbd, 0x61, 0xdd, 0xfe, 0xe8,
                0x1a, 0xca, 0x9c, 0xe6,
            ],
            [
                0x00, 0x81, 0x26, 0x2a, 0x00, 0x00, 0x04, 0x80, 0xdc, 0xbc, 0x9a, 0x3d, 0xa6, 0xfb,
                0xef, 0x5c, 0x1c, 0x0a, 0x55, 0xe4, 0x8a, 0x0e, 0x72, 0x9f, 0x91, 0x84, 0xfc, 0xb1,
                0x40, 0x7c, 0x31, 0x52,
            ],
            [
                0x9d, 0xb2, 0x68, 0xf6, 0xfe, 0x50, 0x03, 0x2a, 0x36, 0x3c, 0x98, 0x01, 0x30, 0x68,
                0x37, 0xfa, 0xfa, 0xbd, 0xf9, 0x57, 0xfd, 0x97, 0xea, 0xfc, 0x80, 0xdb, 0xd1, 0x65,
                0xe4, 0x35, 0xd0, 0xe2,
            ],
            [
                0xdf, 0xd8, 0x36, 0xa2, 0x8b, 0x35, 0x40, 0x23, 0x92, 0x4b, 0x6f, 0xb7, 0xe4, 0x8b,
                0xc0, 0xb3, 0xed, 0x95, 0xee, 0xa6, 0x4c, 0x2d, 0x40, 0x2f, 0x4d, 0x73, 0x4c, 0x8d,
                0xc2, 0x6f, 0x3a, 0xc5,
            ],
            [
                0x91, 0x82, 0x5d, 0xae, 0xf0, 0x1e, 0xae, 0x3c, 0x38, 0xe3, 0x32, 0x8d, 0x00, 0xa7,
                0x7d, 0xc6, 0x57, 0x03, 0x4f, 0x28, 0x7c, 0xcb, 0x0f, 0x0e, 0x1c, 0x9a, 0x7c, 0xbd,
                0xc8, 0x28, 0xf6, 0x27,
            ],
            [
                0x20, 0x5e, 0x47, 0x37, 0xb8, 0x4b, 0x58, 0x37, 0x65, 0x51, 0xd4, 0x4c, 0x12, 0xc3,
                0xc2, 0x15, 0xc8, 0x12, 0xa0, 0x97, 0x07, 0x89, 0xc8, 0x3d, 0xe5, 0x1d, 0x6a, 0xd7,
                0x87, 0x27, 0x19, 0x63,
            ],
            [
                0x32, 0x7f, 0x0a, 0x5f, 0xbb, 0x6b, 0x59, 0x07, 0xde, 0xc0, 0x2c, 0x9a, 0x90, 0x93,
                0x4a, 0xf5, 0xa1, 0xc6, 0x3b, 0x72, 0xc8, 0x26, 0x53, 0x60, 0x5d, 0x1d, 0xcc, 0xe5,
                0x15, 0x96, 0xb3, 0xc2,
            ],
            [
                0xb4, 0x56, 0x96, 0x68, 0x9f, 0x2e, 0xb3, 0x82, 0x00, 0x74, 0x97, 0x55, 0x76, 0x92,
                0xca, 0xac, 0x4d, 0x57, 0xb5, 0xde, 0x9f, 0x55, 0x69, 0xbc, 0x2a, 0xd0, 0x13, 0x7f,
                0xd4, 0x7f, 0xb4, 0x7e,
            ],
            [
                0x66, 0x4f, 0xcb, 0x6d, 0xb4, 0x97, 0x1f, 0x5b, 0x3e, 0x07, 0xac, 0xed, 0xa9, 0xac,
                0x13, 0x0e, 0x9f, 0x38, 0x18, 0x2d, 0xe9, 0x94, 0xcf, 0xf1, 0x92, 0xec, 0x0e, 0x82,
                0xfd, 0x6d, 0x4c, 0xb7,
            ],
            [
                0xf3, 0xfe, 0x00, 0x81, 0x25, 0x89, 0xb7, 0xa7, 0xce, 0x51, 0x54, 0x40, 0x45, 0x64,
                0x33, 0x01, 0x6b, 0x84, 0xa5, 0x9b, 0xec, 0x66, 0x19, 0xa1, 0xc6, 0xc0, 0xb3, 0x7d,
                0xd1, 0x45, 0x0e, 0xd4,
            ],
            [
                0xf2, 0xd8, 0xb5, 0x84, 0x41, 0x0c, 0xed, 0xa8, 0x02, 0x5f, 0x5d, 0x2d, 0x8d, 0xd0,
                0xd2, 0x17, 0x6f, 0xc1, 0xcf, 0x2c, 0xc0, 0x6f, 0xa8, 0xc8, 0x2b, 0xed, 0x4d, 0x94,
                0x4e, 0x71, 0x33, 0x9e,
            ],
            [
                0xce, 0x78, 0x0f, 0xd0, 0x25, 0xbd, 0x41, 0xec, 0x34, 0xeb, 0xff, 0x9d, 0x42, 0x70,
                0xa3, 0x22, 0x4e, 0x01, 0x9f, 0xcb, 0x44, 0x44, 0x74, 0xd4, 0x82, 0xfd, 0x2d, 0xbe,
                0x75, 0xef, 0xb2, 0x03,
            ],
            [
                0x89, 0xcc, 0x10, 0xcd, 0x60, 0x0a, 0xbb, 0x54, 0xc4, 0x7e, 0xde, 0x93, 0xe0, 0x8c,
                0x11, 0x4e, 0xdb, 0x04, 0x11, 0x7d, 0x71, 0x4d, 0xc1, 0xd5, 0x25, 0xe1, 0x1b, 0xed,
                0x87, 0x56, 0x19, 0x2f,
            ],
            [
                0x92, 0x9d, 0x15, 0x46, 0x2b, 0x93, 0x9f, 0xf3, 0xf5, 0x2f, 0x22, 0x52, 0xda, 0x2e,
                0xd6, 0x4d, 0x8f, 0xae, 0x88, 0x81, 0x8b, 0x1e, 0xfa, 0x2c, 0x7b, 0x08, 0xc8, 0x79,
                0x4f, 0xb1, 0xb2, 0x14,
            ],
            [
                0xaa, 0x23, 0x3d, 0xb3, 0x16, 0x28, 0x33, 0x14, 0x1e, 0xa4, 0x38, 0x3f, 0x1a, 0x6f,
                0x12, 0x0b, 0xe1, 0xdb, 0x82, 0xce, 0x36, 0x30, 0xb3, 0x42, 0x91, 0x14, 0x46, 0x31,
                0x57, 0xa6, 0x4e, 0x91,
            ],
            [
                0x23, 0x4d, 0x47, 0x5e, 0x2f, 0x79, 0xcb, 0xf0, 0x5e, 0x4d, 0xb6, 0xa9, 0x40, 0x7d,
                0x72, 0xc6, 0xbf, 0xf7, 0xd1, 0x19, 0x8b, 0x5c, 0x4d, 0x6a, 0xad, 0x28, 0x31, 0xdb,
                0x61, 0x27, 0x49, 0x93,
            ],
            [
                0x71, 0x5a, 0x01, 0x82, 0xc7, 0xdc, 0x80, 0x89, 0xe3, 0x2c, 0x85, 0x31, 0xde, 0xed,
                0x4f, 0x74, 0x31, 0xc0, 0x7c, 0x02, 0x19, 0x5e, 0xba, 0x2e, 0xf9, 0x1e, 0xfb, 0x56,
                0x13, 0xc3, 0x7a, 0xf7,
            ],
            [
                0xae, 0x0c, 0x06, 0x6b, 0xab, 0xc6, 0x93, 0x69, 0x70, 0x0e, 0x1d, 0xd2, 0x6e, 0xdd,
                0xc0, 0xd2, 0x16, 0xc7, 0x81, 0xd5, 0x6e, 0x4c, 0xe4, 0x7e, 0x33, 0x03, 0xfa, 0x73,
                0x00, 0x7f, 0xf7, 0xb9,
            ],
            [
                0x49, 0xef, 0x23, 0xbe, 0x2a, 0xa4, 0xdb, 0xf2, 0x52, 0x06, 0xfe, 0x45, 0xc2, 0x0d,
                0xd8, 0x88, 0x39, 0x5b, 0x25, 0x26, 0x39, 0x1a, 0x72, 0x49, 0x96, 0xa4, 0x41, 0x56,
                0xbe, 0xac, 0x80, 0x82,
            ],
            [
                0x12, 0x85, 0x87, 0x92, 0xbf, 0x8e, 0x74, 0xcb, 0xa4, 0x9d, 0xee, 0x5e, 0x88, 0x12,
                0xe0, 0x19, 0xda, 0x87, 0x45, 0x4b, 0xff, 0x9e, 0x84, 0x7e, 0xd8, 0x3d, 0xb0, 0x7a,
                0xf3, 0x13, 0x74, 0x30,
            ],
            [
                0x82, 0xf8, 0x80, 0xa2, 0x78, 0xf6, 0x82, 0xc2, 0xbd, 0x0a, 0xd6, 0x88, 0x7c, 0xb5,
                0x9f, 0x65, 0x2e, 0x15, 0x59, 0x87, 0xd6, 0x1b, 0xbf, 0x6a, 0x88, 0xd3, 0x6e, 0xe9,
                0x3b, 0x60, 0x72, 0xe6,
            ],
            [
                0x65, 0x6d, 0x9c, 0xcb, 0xaa, 0xe3, 0xd6, 0x55, 0x85, 0x2e, 0x38, 0xde, 0xb3, 0xa2,
                0xdc, 0xf8, 0x05, 0x8d, 0xc9, 0xfb, 0x6f, 0x2a, 0xb3, 0xd3, 0xb3, 0x53, 0x9e, 0xb7,
                0x7b, 0x24, 0x8a, 0x66,
            ],
            [
                0x10, 0x91, 0xd0, 0x5e, 0xb6, 0xe2, 0xf2, 0x97, 0x77, 0x4f, 0xe6, 0x05, 0x35, 0x98,
                0x45, 0x7c, 0xc6, 0x19, 0x08, 0x31, 0x8d, 0xe4, 0xb8, 0x26, 0xf0, 0xfc, 0x86, 0xd4,
                0xbb, 0x11, 0x7d, 0x33,
            ],
            [
                0xe8, 0x65, 0xaa, 0x80, 0x50, 0x09, 0xcc, 0x29, 0x18, 0xd9, 0xc2, 0xf8, 0x40, 0xc4,
                0xda, 0x43, 0xa7, 0x03, 0xad, 0x9f, 0x5b, 0x58, 0x06, 0x16, 0x3d, 0x71, 0x61, 0x69,
                0x6b, 0x5a, 0x0a, 0xdc,
            ],
        ];

        // test value decoding
        let sig = sig.unwrap();
        assert_eq!(sig.c.as_slice(), &ec);
        assert_eq!(sig.y[0].as_slice(), &ey[0]);
        assert_eq!(sig.y[1].as_slice(), &ey[1]);
        assert_eq!(sig.y[2].as_slice(), &ey[2]);
        assert_eq!(sig.y[3].as_slice(), &ey[3]);
        assert_eq!(sig.y[4].as_slice(), &ey[4]);
        assert_eq!(sig.y[5].as_slice(), &ey[5]);
        assert_eq!(sig.y[6].as_slice(), &ey[6]);
        assert_eq!(sig.y[7].as_slice(), &ey[7]);
        assert_eq!(sig.y[8].as_slice(), &ey[8]);
        assert_eq!(sig.y[9].as_slice(), &ey[9]);
        assert_eq!(sig.y[10].as_slice(), &ey[10]);
        assert_eq!(sig.y[11].as_slice(), &ey[11]);
        assert_eq!(sig.y[12].as_slice(), &ey[12]);
        assert_eq!(sig.y[13].as_slice(), &ey[13]);
        assert_eq!(sig.y[14].as_slice(), &ey[14]);
        assert_eq!(sig.y[15].as_slice(), &ey[15]);
        assert_eq!(sig.y[16].as_slice(), &ey[16]);
        assert_eq!(sig.y[17].as_slice(), &ey[17]);
        assert_eq!(sig.y[18].as_slice(), &ey[18]);
        assert_eq!(sig.y[19].as_slice(), &ey[19]);
        assert_eq!(sig.y[20].as_slice(), &ey[20]);
        assert_eq!(sig.y[21].as_slice(), &ey[21]);
        assert_eq!(sig.y[22].as_slice(), &ey[22]);
        assert_eq!(sig.y[23].as_slice(), &ey[23]);
        assert_eq!(sig.y[24].as_slice(), &ey[24]);
        assert_eq!(sig.y[25].as_slice(), &ey[25]);
        assert_eq!(sig.y[26].as_slice(), &ey[26]);
        assert_eq!(sig.y[27].as_slice(), &ey[27]);
        assert_eq!(sig.y[28].as_slice(), &ey[28]);
        assert_eq!(sig.y[29].as_slice(), &ey[29]);
        assert_eq!(sig.y[30].as_slice(), &ey[30]);
        assert_eq!(sig.y[31].as_slice(), &ey[31]);
        assert_eq!(sig.y[32].as_slice(), &ey[32]);
        assert_eq!(sig.y[33].as_slice(), &ey[33]);
    }
}
